package com.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class JdbcUtils {
    //dbutils的数据库操作对象
    private static QueryRunner runner = null;

    static {
        //创建连接池对象
        InputStream is = JdbcUtils.class.getClassLoader().getResourceAsStream("Druid.properties");
        Properties prop = new Properties();
        try {
            prop.load(is);
            DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
            //创建操作对象
            runner=new QueryRunner(dataSource);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查询对象集合
    public static <T> List<T> queryList(Class clazz, String sql, Object... args) {
        //处理实体类的属性和表的字段映射处理器
        BasicRowProcessor brp = new BasicRowProcessor(new GenerousBeanProcessor());
        try {
            return runner.query(sql, new BeanListHandler<T>(clazz, brp), args);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //查询单个对象
    public static <T> T queryOne(Class clazz,String sql,Object ...args){
        //处理实体类的属性和表的字段映射处理器
        BasicRowProcessor brp = new BasicRowProcessor(new GenerousBeanProcessor());
        try {
            return runner.query(sql,new BeanHandler<T>(clazz,brp),args);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //增删改
    public static int update(String sql ,Object ... args){
        try {
            return runner.update(sql, args);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public static <T>T toBean(Map map, Class<T> c){
        T t=null;
        try {
            t = c.newInstance();
            BeanUtils.populate(t,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }
    //查询总数
    public static int queryCount(String sql,Object...args){
        try {
            return runner.query(sql, new ResultSetHandler<Integer>() {
                @Override
                public Integer handle(ResultSet resultSet) throws SQLException {
                    //指向第一行
                    resultSet.next();
                    //返回第一列的整数值
                    return resultSet.getInt(1);
                }
            },args);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

}
